﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Kioskvalt.Common
{
    public class ConfigSection : System.Configuration.IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            Console.WriteLine("Parsing configuration...");

            Configuration cfg = new Configuration();
            Console.WriteLine(section.InnerXml);

            XmlNode n;

            n = section.SelectSingleNode("server/host");
            if (n != null)
                cfg.ServerHost = n.InnerText;

            n = section.SelectSingleNode("server/port");
            if (n != null)
            {
                int i = 0;
                if (int.TryParse(n.InnerText, out i))
                    cfg.ServerPort = i;
            }

            XmlNodeList nl;

            nl = section.SelectNodes("uis/ui");
            for (int k = 0; k < nl.Count; k++)
            {
                n = nl[k];

                UIConfig cu = new UIConfig();

                if (n.Attributes["name"] != null)
                    cu.Name = n.Attributes["name"].Value;

                if (n.Attributes["description"] != null)
                    cu.Description = n.Attributes["description"].Value;

                if (n.Attributes["useragent"] != null)
                    cu.UserAgent = n.Attributes["useragent"].Value;

                if (n.Attributes["priority"] != null)
                {
                    int i = 0;
                    if (int.TryParse(n.Attributes["priority"].Value, out i))
                        cu.Priority = i;
                }


                cfg.UIs.Add(cu);
            }

            nl = section.SelectNodes("devices/device");
            for (int k = 0; k < nl.Count; k++)
            {
                n = nl[k];

                DeviceConfig ic = new DeviceConfig();

                if (n.Attributes["name"] != null)
                    ic.Name = n.Attributes["name"].Value;

                if (n.Attributes["description"] != null)
                    ic.Description = n.Attributes["description"].Value;

                if (n.Attributes["width"] != null)
                {
                    int i = 0;
                    int.TryParse(n.Attributes["width"].Value, out i);
                	ic.Width = i;
                }

                if (n.Attributes["height"] != null)
                {
                    int i = 0;
					int.TryParse(n.Attributes["height"].Value, out i);
					ic.Height = i;
                }

                if (n.Attributes["uis"] != null)
                {
                    string[] tmp = n.Attributes["uis"].Value.Trim().Split(',');
                    for (int j = 0; j < tmp.Length; j++)
                        ic.UIs.Add(tmp[j].Trim());
                }

                cfg.Devices.Add( ic);
            }

            nl = section.SelectNodes("editors/editor");
            for (int k = 0; k < nl.Count; k++)
            {
                n = nl[k];

                EditorConfig ic = new EditorConfig();

                if (n.Attributes["name"] != null)
                    ic.Name = n.Attributes["name"].Value;

                if (n.Attributes["description"] != null)
                    ic.Description = n.Attributes["description"].Value;

				if (n.Attributes["width"] != null)
				{
					int i = 0;
					int.TryParse(n.Attributes["width"].Value, out i);
					ic.Width = i;
				}

				if (n.Attributes["height"] != null)
				{
					int i = 0;
					int.TryParse(n.Attributes["height"].Value, out i);
					ic.Height = i;
				}

                if (n.Attributes["uis"] != null)
                {
                    string[] tmp = n.Attributes["uis"].Value.Trim().Split(',');
                    for (int j = 0; j < tmp.Length; j++)
                        ic.UIs.Add(tmp[j].Trim());
                }

                cfg.Editors.Add(  ic);
            }

            return cfg;
        }
    }
}
